import { createRouter, createWebHistory } from 'vue-router'

import TeacherMange from '@/views/home/TeacherMange.vue'
import StudentMange from '@/views/home/StudentMange.vue'
import CourseMange from '@/views/home/CourseMange.vue'
import EvaluationMassage from '@/views/home/EvaluationMassage.vue'
import StudentInfo from '@/views/home/StudentInfo.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'login', component: () => import('@/views/LoginView.vue') },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/HomeView.vue'),
    },
  ],
})

const routers = await JSON.parse(localStorage.getItem('homeRouters'))

for (let i = 0; i < routers.length; i++) {
  switch (routers[i].component) {
    case 'TeacherMange':
      router.addRoute('home', { path: routers[i].path, component: TeacherMange })
      break
    case 'StudentMange':
      router.addRoute('home', { path: routers[i].path, component: StudentMange })
      break
    case 'CourseMange':
      router.addRoute('home', { path: routers[i].path, component: CourseMange })
      break
    case 'EvaluationMassage':
      router.addRoute('home', { path: routers[i].path, component: EvaluationMassage })
      break
    case 'StudentInfo':
      router.addRoute('home', { path: routers[i].path, component: StudentInfo })
  }
}

router.beforeEach((to, from) => {
  // ...
  // 返回 false 以取消导航
  console.log('router.beforeEach', to, from);
  console.log('router.to.path', to.path);
  console.log('router.getRouters', router.getRoutes());
  console.log('router.hasRoute', router.hasRoute(to.path));

  return true
})

export default router
